package com.wish233.wiliwilisearch.job;

import com.wish233.wiliwiliapi.client.VideoClient;
import com.wish233.wiliwiliapi.domain.vo.VideoVO;
import com.wish233.wiliwilisearch.domain.esdto.VideoEsDTO;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author WHH
 * 运行时同步elasticsearch和MySQL的视频数据
 */
//@Component
@Order(2)
public class SingleJob implements CommandLineRunner {

    @Resource
    private ElasticsearchRestTemplate restTemplate;

    @Resource
    private VideoClient videoClient;

    @Override
    public void run(String... args) throws Exception {
        List<VideoVO> allVideo = videoClient.getAllVideo();
        List<VideoEsDTO> collect =
                    allVideo.stream()
                .map(VideoEsDTO::vo2EsDTO)
                .collect(Collectors.toList());
        restTemplate.save(collect);

    }
}
